package org.clxmm.security03springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author clxmm
 * @Description
 * @create 2021-06-09 8:44 下午
 */
@Service
public class SpringDataUserDetailsService implements UserDetailsService {

    @Autowired
    UserService userService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 模拟数据库查询
        System.out.println("username:" +  username);
        // BCrypt 加密的密码
//        UserDetails userDetails = User.withUsername("zs").password("$2a$10$GKRr/BLdAa9/LRAc1Q.t8OU0c5Fi1BuA52cdpWiPPfJuaS/TxHFQ6").authorities("p1").build();
        org.clxmm.security03springboot.Dao.User user = userService.getUserByUserName(username);

        if (user==null) {
            return null;
        }

        UserDetails userDetails = User.withUsername(user.getUsername()).password(user.getPassword()).authorities("p1").build();

        return userDetails;
    }



}
